/**
 *  *2010-3-31 上午01:17:28  add 
 */
package com.base.myproject.client.ui;

import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.tools.Debug;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.treegrid.EditorTreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridSelectionModel;

/**
 * @author Administrator
 *2010-3-31
 * javazgw@gmail.com
 */
@SuppressWarnings("unchecked")
public class BaseEditorTreeGrid<M extends ModelData> extends EditorTreeGrid<M>{
	
	static BaseEditorTreeGrid _this;

	public BaseEditorTreeGrid(TreeStore store, ColumnModel cm) {
		    super(store, cm);
		    
		    TreeGridSelectionModel<M> tgm =   new TreeGridSelectionModel<M>();
		    //tgm.addSelectionChangedListener(listener)
		    tgm.addListener(Events.SelectionChange, getListener());
		   
		    setSelectionModel(tgm);
		    _this = this;
		  }

	 int sync = 0;
		Listener<BaseEvent> defaultlistener;

		Listener<BaseEvent> getListener() {
			if (defaultlistener != null) {
				return defaultlistener;
			}

			defaultlistener = new Listener<BaseEvent>() {
				@SuppressWarnings("unchecked")
				public void handleEvent(BaseEvent be) {
					sync++;
					try {
						if (sync == 1) {
							if (be.getType() == Events.Select) {
								if (be.getSource().equals("")) {
									
								}
								else 	if (be.getSource().equals("")) {
									
								}

							} else if (be.getType() == Events.Hide) {

							} else if (be.getType() == Events.Change) {
								if (be.getSource().equals("")) {

								}
							} else if (be.getType() == Events.KeyUp) {
								if (be instanceof ComponentEvent) {
									if (((ComponentEvent) be).getKeyCode() == 13) // 回车
									{
									}
								}
							}
							else if (be.getType() == Events.SelectionChange) {
								Debug.printf("SelectionChange=");
								if (store != null) {
//									store.f
//									// Debug.printf(((GridSelectionModel)be.getSource()).getSelectedItem().get("cname"));
//									int row = store.indexOf(((GridSelectionModel) be.getSource())
//											.getSelectedItem());
//									// Debug.printf("row="+row);
//									if (row >= 0)
//										store.gotorow(row);
								//	System.out.println(((TreeGridSelectionModel)be.getSource()).getSelectedItems().size());
									
									if(((TreeGridSelectionModel)be.getSource()).getSelectedItems().size()>0)
									{
									System.out.println(store.getModels().indexOf((M)((TreeGridSelectionModel)be.getSource()).getSelectedItems().get(0)));
									if(_this.getTreeStore() instanceof BaseTreeStore)
									{
										
										int index = ((BaseTreeStore) _this.getTreeStore()).getAllItems().indexOf((M)((TreeGridSelectionModel)be.getSource()).getSelectedItems().get(0));
										
										((BaseTreeStore) _this.getTreeStore()).gotorow(index);
										System.out.println(((BaseTreeStore) _this.getTreeStore()).getValue("icode"));
									}
									}
									
								}
							}
						}
					} finally {
						sync--;
					}
				}

			};
			return defaultlistener;
		}
}
